package org.dingxf.util;

import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Set;

public class JsonObject<T> {

    /**
    *   将map集合转换为json
    */
    public String getJsonString(Map<String,String> map){

        if (map==null) return "集合为空不可转换！";

        //将map集合转换为set
        Set<String> set = map.keySet();

        StringBuffer buffer = new StringBuffer();

        if (map.size()==1){
            String content = "";
            for (String s : set) {
                return "{"+s+":\""+map.get(s)+"\"}";
            }
            return content;
        }else {
            buffer.append("[");
            int code = 0;
            //遍历set集合
            for (String s : set) {
                if (code==map.size()-1){
                    buffer.append("{" + s + ":\"" + map.get(s) + "\"}");
                }else {
                    //通过键获取值
                    buffer.append("{" + s + ":\"" + map.get(s) + "\"},");
                    code++;
                }
            }
            buffer.append("]");
        }
        return buffer.toString();
    }



    public <T> String getJsonString(T e) throws Exception{
        Class<?> aClass = e.getClass();
        //获取参数
        Field[] fields = aClass.getDeclaredFields();
        //获取方法
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }

        return null;
    }

}
